﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VQData;
using System.Data;
using VQWEB.common;
using System.Data.Linq;
namespace VQWEB.administrator.sources
{
    public class NewsData
    {
        public NewsData() { }

        #region core_news
        //add edit
        public int InsertOrUpdateNews(core_new obj)
        {
            try
            {
                int Id = 0;
                using (CoreModelDataContext context = new CoreModelDataContext())
                {
                    if (obj.news_id > 0)
                    {
                        core_new flag = GetNewsById(obj.news_id);                        
                        if (flag != null)
                        {
                            context.core_news.Attach(obj);
                            context.Refresh(RefreshMode.KeepCurrentValues, obj);
                            context.SubmitChanges();
                            Id = obj.news_id;
                        }
                    }
                    else
                    {
                        context.core_news.InsertOnSubmit(obj);
                        context.SubmitChanges();
                        Id = obj.news_id;
                        
                    }
                   
                }
                return Id;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }  
        }
        
        public void DeleteNews(List<int> array_id)
        {
            using (CoreModelDataContext context = new CoreModelDataContext())
            {
                var query = from n in context.core_news
                            where array_id.Contains(n.news_id)
                            select n;
                if (query.Any())
                {
                    context.core_news.DeleteAllOnSubmit(query);
                    context.SubmitChanges();
                }
            }
        }
        //View core_news by Id
        public core_new  GetNewsById(int id)
        {
            using (CoreModelDataContext context = new CoreModelDataContext())
            {
                var Obj = context.core_news.Where(n => n.news_id == id).SingleOrDefault();
                if (Obj != null)
                    return Obj;
                return null;
            }
        }
        //List View core_news
        public DataTable ViewNews()
        {
            using (CoreModelDataContext context = new CoreModelDataContext())
            {
                var query = from n in context.core_news
                            select new
                            {
                                news_id = n.news_id,
                                news_title = n.news_title,
                                news_description = n.news_description,
                                news_content = n.news_content,
                                news_hit = n.news_hit,
                                news_date = n.news_date,
                                news_active = n.news_active
                                //category_name = n.core_news_category.category_name,
                            };
                if (query.Any())
                    return HelperData.LINQToDataTable(query);
                return new DataTable();
            }
        }

        public DataTable ViewNews(int Id)
        {
            using (CoreModelDataContext context = new CoreModelDataContext())
            {
                var query = from n in context.core_news
                            select new
                            {
                                news_id = n.news_id,
                                news_title = n.news_title,
                                news_description = n.news_description,
                                news_content = n.news_content,
                                news_hit = n.news_hit,
                                news_date = n.news_date,
                                news_active = n.news_active
                                //category_name = n.core_news_category.category_name,
                            };
                if (query.Any())
                {
                    if (Id > 0)
                        query = query.Where(n => n.news_id.Equals(Id));
                    return HelperData.LINQToDataTable(query);
                }
                else
                    return new DataTable();
            }
        }

        public void LockUnLockNews(List<int> array_id) {
            using (CoreModelDataContext context = new CoreModelDataContext())
            {
                var query = from c in context.core_news
                            where array_id.Contains(c.news_id)
                            select c;
                if (query.Any())
                {
                    foreach (var item in query)
                    {
                        item.news_active = (item.news_active == true) ? false : true;
                        context.SubmitChanges();
                    }
                }

            }
        }
        
        #endregion


    }
}